<?php

namespace common\service;

use common\model\TaskWorkOrderUserModel;
use support\EntityTaskFilterParser;
use support\SingletonTrait;

class TaskLibraryTaskWorkOrderUserService
{
    use SingletonTrait;
    use EntityTaskFilterParser;

    /**
     * 查询工单用户任务列表
     * @param $userId
     * @param $fields
     * @param $page
     * @return array|mixed|string
     */
    public function getTaskWorkOrderUserList($userId, $fields, $page)
    {
        if (empty($userId)) {
            return [];
        }
        $unClosedTaskStatusIds = StatusService::getInstance()->byCodeMap(['closed'], true, 'id');
        $fields = $fields ?? ['task.id'];
        $taskWorkOrderUserList = model(TaskWorkOrderUserModel::class)
            ->join('task_work_order on task_work_order.id = task_work_order_user.work_order_id')
            ->join('task on task.id = task_work_order.task_id')
            ->field($fields)
            ->where(['task_work_order_user.user_id' => $userId])
            ->where(["JSON_UNQUOTE(JSON_EXTRACT(task.json,'$.task_status'))" => ['IN', $unClosedTaskStatusIds]])
            ->page(...$page)
            ->select();
        if (empty($taskWorkOrderUserList)) {
            return [];
        }
        return $taskWorkOrderUserList;
    }

}
